package controller

import (
	"leeframe/internal/pkg/http"
	"leeframe/internal/pkg/logger"
	"leeframe/internal/pkg/validation"
	"leeframe/internal/schema"
	"leeframe/internal/service/user_service"

	"github.com/gin-gonic/gin"
)

var User = new(userApi)

type userApi struct{}

// List 用户列表
// @Summary 用户列表
// @Description 用户列表
// @Tags 公共接口
// @Accept mpfd
// @Produce json
// @Param Request query schema.UserListReq true "请求信息"
// @Success 200 {object} http.Response{data=schema.PaginationResp{list=[]schema.UserListData}}
// @Router /api/user/list [get]
// @Author: lee
// @Date: 2021-11-29 16:42:48
func (a *userApi) List(c *gin.Context) {
	appGin := http.Gin{C: c}
	req := new(schema.UserListReq)

	if err := c.ShouldBindQuery(&req); err != nil {
		logger.ErrorN(c, "get user list err: ", validation.Error(err))
		appGin.Response(http.InvalidParams, validation.Error(err), nil)
		return
	}

	resListData, err := user_service.List(c, req)
	if err != nil {
		logger.ErrorN(c, err)
		appGin.Response(http.ERROR, err.Error(), nil)
		return
	}

	appGin.Response(http.SUCCESS, "OK", resListData)
}

// Info 获取用户详情
// @Summary 获取用户详情
// @Description 获取用户详情
// @Tags 公共接口
// @Accept mpfd
// @Produce json
// @Param Request query schema.UserInfoReq true "请求信息"
// @Success 200 {object} http.Response{data=schema.UserInfoData}
// @Router /api/user/info [get]
// @Author: lee
// @Date: 2021-11-30 11:19:28
func (a *userApi) Info(c *gin.Context) {
	var (
		appGin = http.Gin{C: c}
		req    = new(schema.UserInfoReq)
	)

	if err := c.ShouldBindQuery(&req); err != nil {
		logger.ErrorN(c, "get user info err: ", validation.Error(err))
		appGin.Response(http.InvalidParams, validation.Error(err), nil)
		return
	}

	info, err := user_service.Info(c, req.ID)
	if err != nil {
		logger.ErrorN(c, "get user info err: ", err.Error())
		appGin.Response(http.ERROR, err.Error(), nil)
		return
	}

	appGin.Response(http.SUCCESS, "OK", info)
}

// Create 创建用户
// @Summary 创建用户
// @Description 创建用户
// @Tags 管理员接口
// @Accept json
// @Produce json
// @Param IsAdmin header boolean true "是否是管理员"
// @Param Request body schema.UserCreateReq true "请求信息"
// @Success 200 {object} http.Response{data=integer}
// @Router /api/user/create [post]
// @Author: lee
// @Date: 2021-11-30 11:19:28
func (a *userApi) Create(c *gin.Context) {
	var (
		appGin = http.Gin{C: c}
		req    = new(schema.UserCreateReq)
	)

	if err := c.ShouldBindJSON(&req); err != nil {
		logger.ErrorN(c, "create user err: ", validation.Error(err))
		appGin.Response(http.InvalidParams, validation.Error(err), nil)
		return
	}

	id, err := user_service.Create(c, req.Username)
	if err != nil {
		logger.ErrorN(c, "create user err: ", err.Error())
		appGin.Response(http.ERROR, err.Error(), nil)
		return
	}

	appGin.Response(http.SUCCESS, "OK", id)
}

// Delete 删除用户
// @Summary 删除用户
// @Description 删除用户
// @Tags 管理员接口
// @Accept json
// @Produce json
// @Param IsAdmin header boolean true "是否是管理员"
// @Param Request body schema.UserDeleteReq true "请求信息"
// @Success 200 {object} http.Response
// @Router /api/user/delete [post]
// @Author: lee
// @Date: 2021-12-23 17:22:01
func (a *userApi) Delete(c *gin.Context) {
	var (
		appGin = http.Gin{C: c}
		req    = new(schema.UserDeleteReq)
	)

	if err := c.ShouldBindJSON(&req); err != nil {
		logger.ErrorN(c, "delete user err: ", validation.Error(err))
		appGin.Response(http.InvalidParams, validation.Error(err), nil)
		return
	}

	err := user_service.Delete(c, req.ID)
	if err != nil {
		logger.ErrorN(c, "delete user err: ", err.Error())
		appGin.Response(http.ERROR, err.Error(), nil)
		return
	}

	appGin.Response(http.SUCCESS, "OK", nil)
}
